/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ik.RestauranteBar.SMO.Ventanas.Mantenimientos;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorPerfiles;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorUsuarios;
import org.ik.RestauranteBar.SMO.Ventanas.Principal;
import org.ik.RestauranteBar.SMO.Ventanas.Principal;

/**
 *
 * @author Malkavian
 */
public class Usuarios extends javax.swing.JFrame {

    org.ik.RestauranteBar.SMO.Encapsulados.Usuarios u = new org.ik.RestauranteBar.SMO.Encapsulados.Usuarios();
    org.ik.RestauranteBar.SMO.Encapsulados.Usuarios usua = new org.ik.RestauranteBar.SMO.Encapsulados.Usuarios();
    HashMap<String, org.ik.RestauranteBar.SMO.Encapsulados.Usuarios> lista_usuarios = new HashMap<>();
    HashMap<String, org.ik.RestauranteBar.SMO.Encapsulados.Perfil> lista_permisos = new HashMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    int contador = 0;
    List<org.ik.RestauranteBar.SMO.Encapsulados.Usuarios> usuarios = null;
    List<org.ik.RestauranteBar.SMO.Encapsulados.Perfil> permisos = null;

    public Usuarios(org.ik.RestauranteBar.SMO.Encapsulados.Usuarios u) {

        this.u = u;
        initComponents();

        try {

            llenarUsuarios();

            permisos = ManejadorPerfiles.getInstance().getPerfiles();
            combo_permisos.removeAllItems();
            contador = 0;

            for (org.ik.RestauranteBar.SMO.Encapsulados.Perfil per : permisos) {


                lista_permisos.put("" + contador, per);
                contador++;
                combo_permisos.addItem(per.getDescripcion());

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        texto_contrasena.setText("");
        texto_usuario.setText("");
        combo_permisos.setSelectedIndex(0);
        Dimension d = new Dimension(this.getSize());
        Dimension g = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(g.width / 2 - d.width / 2, g.height / 2 - d.height / 2, d.width, d.height);

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        texto_usuario = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        texto_contrasena = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        combo_permisos = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        boton_guardar = new javax.swing.JButton();
        boton_borrar = new javax.swing.JButton();
        boton_cerrar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        combo_usuarios = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("SMP - Informaciones Usuarios");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informaciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Usuario:");

        texto_usuario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                texto_usuarioActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Contraseña:");

        texto_contrasena.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Permiso:");

        combo_permisos.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_permisos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(texto_usuario)
                    .addComponent(texto_contrasena)
                    .addComponent(combo_permisos, 0, 122, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(texto_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(texto_contrasena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(combo_permisos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        boton_guardar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_guardar.setText("Guardar");
        boton_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_guardarActionPerformed(evt);
            }
        });

        boton_borrar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_borrar.setText("Borrar");
        boton_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_borrarActionPerformed(evt);
            }
        });

        boton_cerrar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_cerrar.setText("Cerrar");
        boton_cerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_cerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boton_guardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(boton_borrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(boton_cerrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(boton_guardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boton_borrar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boton_cerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Usuarios Existentes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        combo_usuarios.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_usuarios.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        combo_usuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_usuariosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(combo_usuarios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(combo_usuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/Reposteria/SMP/Recursos/user3.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/Reposteria/SMP/Recursos/user4.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(22, 22, 22))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void texto_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_texto_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_texto_usuarioActionPerformed

    private void boton_cerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_cerrarActionPerformed
        Principal p = new Principal(u);
        dispose();
    }//GEN-LAST:event_boton_cerrarActionPerformed

    private void combo_usuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_usuariosActionPerformed

        if (lista_usuarios.get("" + combo_usuarios.getSelectedIndex()) != null) {

            usua = lista_usuarios.get("" + combo_usuarios.getSelectedIndex());
            texto_usuario.setText(usua.getUsuario());
            texto_contrasena.setText(usua.getContrasena());
            combo_permisos.setSelectedItem(usua.getPermisosidpermiso().getDescripcion());
        }
    }//GEN-LAST:event_combo_usuariosActionPerformed

    private void boton_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_guardarActionPerformed
        if (texto_contrasena.getText().isEmpty() || texto_usuario.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Favor llenar las informaciones solicitadas.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            usua.setUsuario(texto_usuario.getText());
            usua.setContrasena(texto_contrasena.getText());
            usua.setFechacreacion(new Date());
            usua.setPermisosidpermiso(lista_permisos.get(""+combo_permisos.getSelectedIndex()));

            ManejadorUsuarios.getInstance().insertarUsuarios(usua);
            llenarUsuarios();

        }
    }//GEN-LAST:event_boton_guardarActionPerformed

    private void boton_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_borrarActionPerformed

        if (texto_contrasena.getText().isEmpty() || texto_usuario.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Favor seleccionar algun usuario.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            int confirmado = JOptionPane.showConfirmDialog(null, "¿Deseas borrar este usuario?");

            if (JOptionPane.OK_OPTION == confirmado) {

                ManejadorUsuarios.getInstance().borrarUsuario(usua);
                texto_usuario.setText("");
                texto_contrasena.setText("");
                combo_permisos.setSelectedIndex(0);
                llenarUsuarios();

            }
        }

    }//GEN-LAST:event_boton_borrarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_borrar;
    private javax.swing.JButton boton_cerrar;
    private javax.swing.JButton boton_guardar;
    private javax.swing.JComboBox combo_permisos;
    private javax.swing.JComboBox combo_usuarios;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField texto_contrasena;
    private javax.swing.JTextField texto_usuario;
    // End of variables declaration//GEN-END:variables

    public void llenarUsuarios() {

        usuarios = ManejadorUsuarios.getInstance().getUsuarios();
        combo_usuarios.removeAllItems();
        contador = 0;

        for (org.ik.RestauranteBar.SMO.Encapsulados.Usuarios us : usuarios) {


            lista_usuarios.put("" + contador, us);
            contador++;
            combo_usuarios.addItem(us.getUsuario());

        }

    }
}
